package cn.edu.zjgsu.ito.controller;

import java.util.List;

import cn.edu.zjgsu.ito.model.StudentTimetable;
import cn.edu.zjgsu.ito.service.ServiceFactory;
import cn.edu.zjgsu.ito.service.StudentScheduleService;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class StudentScheduleController extends Controller {

	StudentScheduleService studentScheduleService=ServiceFactory.createStudentScheduleService();

	/**
	 * 学生课表查询初始化
	 * 黄凯航
	 */
	public void index()
	{
        try{
		String passportNo=getSessionAttr("passportNo");
        //System.out.println("!"+passportNo);
		String current="";
		String error="1";
        String tName="";
		List<Record> terms=Db.find("select * from term ");
		List<Record> term=Db.find("select * from term where current=?",1);
		current=term.get(0).get("termValue");
        //System.out.println("!!"+current);
		List<StudentTimetable> timetables=StudentTimetable.dao.find("select * from student_timetable where passportNo=? and termValue=?",passportNo,current);
		//System.out.println("!!!"+timetables);
        if(timetables.size()==0)
			error="0";
        else
        {
            for(int i=0;i<timetables.size();i++)
            {
               tName=timetables.get(i).getStr("tName");
               //System.out.println("tName="+tName);
               System.out.println("length="+tName.length());
               if(tName.length()!=0)
               timetables.get(i).set("tName", tName.substring(0, (tName.length()-1)));
               //System.out.println("tName=="+tName);
            }
        }
		setAttr("studentTimetable",timetables);
		setAttr("term",terms);
		setAttr("error",error);
		//renderJson();
		render("schedule.html");
        }catch(Exception e){
            e.printStackTrace();
        }
	}
	/**
	 * 学生课表搜索
	 * 黄凯航
	 */
	public void search()
	{	
		String passportNo=getSessionAttr("passportNo");
		String term=getPara("term");
		List<Record> terms=Db.find("select * from term where invalid=?",0);
		List<StudentTimetable> timetables=StudentTimetable.dao.find("select * from student_timetable where passportNo=? and termValue=?",passportNo,term);

		setAttr("term",terms);
		setAttr("studentTimetable",timetables);
		render("schedule.html"); 		

	}
	/**
	 * 异步学生课表搜索
	 * 黄凯航
	 */
	public void ajaxsearch()
	{
		String passportNo=getSessionAttr("passportNo");
        //System.out.println("!"+passportNo);
		String termValue=getPara("termValue");
        //System.out.println("!!"+termValue);
		String error="1";
        String tName="";
		List<StudentTimetable> studentTimetable=StudentTimetable.dao.find("select * from student_timetable where termValue=? and passportNo=?",termValue,passportNo);
		//System.out.println("!!!"+studentTimetable);
        if(studentTimetable.size()==0)
			error="0";
        else
        {
            for(int i=0;i<studentTimetable.size();i++)
            {
              tName=studentTimetable.get(i).getStr("tName");
               //System.out.println("tName="+tName);
               System.out.println("length="+tName.length());
               if(tName.length()!=0)
               studentTimetable.get(i).set("tName", tName.substring(0, (tName.length()-1)));
               //System.out.println("tName=="+tName);
            }
        }
		setAttr("studentTimetable",studentTimetable);
		setAttr("error",error);
		renderJson(studentTimetable);

	}
}
